<template>
  <div class="home-container">
    <el-header class="home-header">
      <div class="header-content">
        <div class="header-left">
          <img src="@/assets/logo.svg" alt="Logo" class="logo">
          <h1 class="title">IoT 系统管理平台</h1>
        </div>
        <div class="header-right">
          <el-button type="danger" @click="logout" size="large">退出登录</el-button>
        </div>
      </div>
    </el-header>

    <el-main class="home-main">
      <el-card class="stats-card">
        <div class="stats-container">
          <div class="stat-item">
            <div class="stat-number">{{ users.length }}</div>
            <div class="stat-label">总用户数</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ activeUsers }}</div>
            <div class="stat-label">活跃用户</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ inactiveUsers }}</div>
            <div class="stat-label">未活跃用户</div>
          </div>
        </div>
      </el-card>

      <el-card class="user-list-card">
        <template #header>
          <div class="card-header">
            <span class="card-title">所有用户信息</span>
            <el-button type="primary" @click="refreshUsers">刷新数据</el-button>
          </div>
        </template>

        <el-table :data="users" stripe style="width: 100%" border>
          <el-table-column prop="user_id" label="用户ID" width="120" align="center" sortable />
          <el-table-column prop="username" label="用户名" width="140" align="center" sortable />
          <el-table-column prop="nickname" label="昵称" width="140" align="center" sortable />
          <el-table-column prop="email" label="邮箱" width="220" align="center" />
          <el-table-column prop="phone" label="电话" width="160" align="center" />
          <el-table-column prop="isActive" label="状态" width="120" align="center">
            <template #default="{ row }">
              <el-tag v-if="row.isActive" type="success" effect="dark">激活</el-tag>
              <el-tag v-else type="warning" effect="dark">未激活</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </el-main>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const users = ref([])
const loading = ref(false)
const error = ref('')

// 计算属性：活跃用户数
const activeUsers = computed(() => {
  return users.value.filter(user => user.isActive).length
})

// 计算属性：未活跃用户数
const inactiveUsers = computed(() => {
  return users.value.filter(user => !user.isActive).length
})

// 获取所有用户信息
const fetchUsers = async () => {
  try {
    loading.value = true
    error.value = ''
    const response = await axios.get('/users') // 假设后端提供/users接口
    users.value = response.data.data || []
  } catch (err) {
    error.value = '获取用户列表失败，请稍后重试'
    console.error('获取用户列表错误:', err)
  } finally {
    loading.value = false
  }
}

// 页面加载时获取用户数据
onMounted(() => {
  fetchUsers()
})

// 刷新用户列表
const refreshUsers = () => {
  fetchUsers()
}

// 退出登录
const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('user')
  router.push('/')
}
</script>

<style scoped>
.home-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f0f2f5;
}

.home-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 40px;
  height: 80px;
  box-sizing: border-box;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.logo {
  width: 50px;
  height: 50px;
  object-fit: contain;
}

.title {
  color: white;
  font-size: 24px;
  font-weight: 600;
  margin: 0;
}

.header-right {
  display: flex;
  align-items: center;
}

.home-main {
  padding: 40px;
  flex: 1;
  overflow: auto;
}

.stats-card {
  margin-bottom: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.stats-container {
  display: flex;
  gap: 40px;
  padding: 20px 0;
  justify-content: space-around;
}

.stat-item {
  text-align: center;
}

.stat-number {
  font-size: 36px;
  font-weight: 700;
  color: #303133;
  margin-bottom: 8px;
}

.stat-label {
  font-size: 18px;
  color: #909399;
}

.user-list-card {
  margin-top: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0;
}

.card-title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
}

.el-table {
  margin-top: 20px;
  border-radius: 8px;
  overflow: hidden;
}

.el-table th {
  background-color: #f5f7fa;
  font-weight: 600;
}

.el-table td {
  padding: 16px;
}

.el-tag {
  font-size: 14px;
  font-weight: 500;
}
</style>